package nowCoder;

/**
 * Created by Ghost on 2015/3/25.
 * A message containing letters fromA-Zis being encoded to numbers using the following mapping:
 'A' -> 1
 'B' -> 2
 ...
 'Z' -> 26
 Given an encoded message containing digits, determine the total number of ways to decode it.
 For example,
 Given encoded message"12", it could be decoded as"AB"(1 2) or"L"(12).
 The number of ways decoding"12"is 2.
 */
public class Decode_ways {
    private boolean judgeCode(String code){
        if(code.charAt(0)=='0')
            return false;
        int a = Integer.parseInt(code.trim());
        if(a>0 && a<=26)
            return true;
        else
            return false;
    }
    public int numDecodings(String s) {
        if(s==null || s.length()==0 || !judgeCode(""+s.charAt(0)))
            return 0;
        int[] w = new int[s.length()+1];
        w[0] = 1;
        w[1] = 1;
        char[] chars = s.toCharArray();

        for(int i=2;i<=chars.length;i++){
            int rst1 = judgeCode(""+chars[i-1])?w[i-1]*1:0;
            int rst2 = judgeCode(""+chars[i-2]+chars[i-1])?w[i-2]*1:0;
            w[i] = rst1+rst2;
        }

        return w[s.length()];
    }
    public static void main(String[] args){
        Decode_ways decode_ways = new Decode_ways();
        System.out.println(decode_ways.numDecodings("101"));
    }
}
